package com.chenhao.spingcloud.chenhaoeurekauser.service.impl;

import com.chenhao.spingcloud.chenhaoeurekauser.mapper.ProductMapper;
import com.chenhao.spingcloud.chenhaoeurekauser.model.Product;
import com.chenhao.spingcloud.chenhaoeurekauser.pojo.User;
import com.chenhao.spingcloud.chenhaoeurekauser.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @return
 * @author chenhao
 *
 * @date 2020/4/9 17:44
 */
@Service
public class UserServiceImpl implements UserService {

    private List<User> userList;
    @Resource
    private ProductMapper productMapper;

    @Override
    public User getUser(Long id) {
        List<User> list = userList.stream().filter(userItem -> userItem.getId().equals(id)).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }


    @Override
    public Object listAll(int page, int size) {
        PageHelper.startPage(page, size);
        List<Product> userList = productMapper.listAll();
        PageInfo <Product> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public int insert(Product user) {
        return productMapper.insert(user);
    }

    @Override
    public int remove(Integer userId) {
        return productMapper.remove(userId);
    }

    @Override
    public int update(Product user) {
        return productMapper.update(user);
    }

    /**
     *
     *乐观锁实现
     *
      * @return java.util.Map<java.lang.String,java.lang.String>
     * @author chenhao
     *
     * @date 2020/4/14 12:50
     */
    @Transactional
    @Override
    public Map <String,String> optimisticLock(int userid, int id){
        Map<String,String> dataMap = new HashMap <String,String>();
        Product user =  productMapper.getUserById(userid);
        Integer seckillnum  = user.getSkillNum();
        if(seckillnum>=user.getNum()){
            System.out.println("卖光了,谢谢惠顾！！");
            dataMap.put("flag","fail");
            dataMap.put("data","卖光了,谢谢惠顾！！");
            return dataMap;
        }
        seckillnum++;
        Product seckillProductInfoUpate = new Product();
        seckillProductInfoUpate.setId(id);
        seckillProductInfoUpate.setSkillNum(seckillnum);
        seckillProductInfoUpate.setVersion(user.getVersion());
        productMapper.pessimismLock(seckillProductInfoUpate);
        dataMap.put("flag","success");
        dataMap.put("data","秒杀成功");

        return dataMap;


    }

     /**
     * 悲观锁实现
     * @param userid
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Map<String,String> pessimismLock(int userid, int id){
        Map<String,String> dataMap = new HashMap<String,String>();
        Product user =  productMapper.selectForUpdate(id);
        Integer seckillnum = user.getSkillNum();
        if(seckillnum>=user.getNum()){
            System.out.println("卖光了,谢谢惠顾！！");
            dataMap.put("flag","fail");
            dataMap.put("data","卖光了,谢谢惠顾！！");
            return dataMap;
        }
        seckillnum++;
        Product seckillProductInfoUpate = new Product();
        seckillProductInfoUpate.setId(id);
        seckillProductInfoUpate.setSkillNum(seckillnum);
        productMapper.updateByid(seckillProductInfoUpate);
        dataMap.put("flag","success");
        dataMap.put("data","秒杀成功");

        return dataMap;
    }



}
